#####################
########INDEX########
#####################

#MARIANA CARMO DUARTE

#AUGUST 2024

#Libraries
library(foreign)
library(tidyverse)
library(readstata13)
library(haven)

# load data
setwd("")
politicisation <- read_sav("PoliticalParties_MARPOR.sav")

#Parties in parliament
politicisation <- subset(politicisation, seat_share > 0)

#Create the grouping variable countryear
politicisation$countryear <- paste0(politicisation$country, politicisation$electionyear)

######################
#POLITICISATION INDEX#
######################

#Remove NA's
politicisation <- politicisation%>%drop_na(posEU, salEU)

#Dalton Index
politicisation$averpos <- ave(politicisation$posEU, politicisation$countryear, FUN = mean)
politicisation$v.distance <- politicisation$seat_share * (politicisation$posEU - politicisation$averpos)^2
politicisation$sum.dist <- ave(politicisation$v.distance, politicisation$countryear, FUN = sum)
politicisation$Dalton <- sqrt(politicisation$sum.dist)
summary(politicisation$Dalton)

#Dalton Index (salience)

politicisation$s.v.distance <- politicisation$salEU * politicisation$v.distance
politicisation$sum.s.dist <- ave(politicisation$s.v.distance, politicisation$countryear, FUN = sum)
politicisation$salience.Dalton <- sqrt(politicisation$sum.s.dist)

#POLITICISATION INDEX

#POLARISATION
politicisation$averpos_p <- ave(politicisation$posEU, politicisation$countryear, FUN = mean)
politicisation$dist_pol <- (politicisation$posEU - politicisation$averpos_p)^2
politicisation$w.dist_pol <- politicisation$seat_share*politicisation$dist_pol
politicisation$sum_p <- ave(politicisation$w.dist_pol, politicisation$countryear, FUN = sum)
politicisation$eu_polarisation_ps <- sqrt(politicisation$sum_p)
summary(politicisation$eu_polarisation_ps)

#SALIENCE
politicisation$salience.w <- politicisation$salEU*politicisation$seat_share
politicisation$eu_salience_ps <- ave(politicisation$salience.w, politicisation$countryear, FUN = sum)/100
summary(politicisation$eu_salience_ps)
summary(politicisation$salEU)

#POLITICISATION
politicisation$politicisation <- politicisation$eu_polarisation_ps*politicisation$eu_salience_ps
summary(politicisation$politicisation)
politicisation$politicisation2 <- politicisation$eu_polarisation_ps2*politicisation$eu_salience_ps2

setwd("")
write.csv2(politicisation, "PoliticalParties_MARPOR_ELECTIONS.csv")
write_sav(politicisation, "PoliticalParties_MARPOR_ELECTIONS.sav")
